package ebcs.database.service.impl;

import ebcs.database.bean.BsNeedInfor;
import ebcs.database.bean.BsUserDetailInfo;
import ebcs.database.bean.BsUserInfor;
import ebcs.database.bean.PaUserRole;
import ebcs.database.mybatis.dao.BsNeedInforMapper;
import ebcs.database.mybatis.dao.BsNeedInforMapperSelf;
import ebcs.database.mybatis.dao.BsUserInforMapper;
import ebcs.database.mybatis.dao.BsUserInforMapperSelf;
import ebcs.database.mybatis.dao.PaUserRoleMapper;
import ebcs.database.service.BsUserInforServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import javax.annotation.Resource;

@Service
public class BsUserInforServiceImpl implements BsUserInforServiceImp {


    @Autowired
    private BsUserInforMapper userInforDao;
    @Autowired
    private BsUserInforMapperSelf userInforSelfDao;
    @Autowired
    private BsNeedInforMapperSelf needSelfDao;
    @Autowired
    private PaUserRoleMapper paUserRoleMapper;

    @Override
    public BsUserInfor selectUserInforByPhoneNo(String phoneNo) {
        BsUserInfor user = new BsUserInfor();
        user.setPhoneNo(phoneNo);
        List<BsUserInfor> bsUserList = this.userInforSelfDao.selectUserInforByPhoneNo(user);
        if (bsUserList.size() > 0) {
            return bsUserList.get(0);
        }
        return null;
    }

    @Override
    public List<BsUserInfor> selectUserInforsByPhoneNo(String phoneNo) {
        BsUserInfor user = new BsUserInfor();
        user.setPhoneNo(phoneNo);
        return this.userInforSelfDao.selectUserInforByPhoneNo(user);
    }

    @Override
    public List<BsNeedInfor> searchNeedInforByPhoneNo(BsUserInfor user) {
        List<BsNeedInfor> bsNeedList = this.needSelfDao.selectByPhoneNo(user);
        if (bsNeedList.size() > 0) {
            return bsNeedList;
        }
        return null;
    }

    @Override
    public List<BsNeedInfor> selectByNeedId(String needId) {
        BsNeedInfor bean = new BsNeedInfor();
        bean.setRouid(needId);
        List<BsNeedInfor> bsNeedList = this.needSelfDao.selectByNeedId(bean);
        if (bsNeedList.size() > 0) {
            return bsNeedList;
        }
        return null;
    }

    @Override
    public String selectUserMaxNoByTeamNo(BsUserInfor user) {
        BsUserInfor rsBean = this.userInforSelfDao.selectUserMaxNoByTeamNo(user);
        if (rsBean == null) {
            return "LX"+user.getProTeamNo() + "000";
        } else {
            return rsBean.getUserNo();
        }
    }

    @Override
    public int insertUserInfor(BsUserInfor user) {
    	
        this.userInforDao.insert(user);
        //int i = 1/0;
        //添加用户权限到数据库中
        PaUserRole paUserRole = new PaUserRole();
        paUserRole.setUserNo(user.getUserNo());
        paUserRole.setRoleNo("12");
        return paUserRoleMapper.insert(paUserRole);
    }

    @Override
    public BsUserInfor selectUserInforByOpenid(String openid) {
        BsUserInfor record = new BsUserInfor();
        record.setOpenid(openid);
        return this.userInforSelfDao.selectUserInforByOpenid(record);
    }

    @Override
    public int updateUserOpenIdByPhoneNo(String openid, String phoneNo) {
        BsUserInfor record = new BsUserInfor();
        record.setOpenid(openid);
        record.setPhoneNo(phoneNo);
        return this.userInforSelfDao.updateUserOpenIdByPhoneNo(record);
    }

    @Override
    public List<BsUserInfor> selectUserInforByWechatUser() {

        return this.userInforSelfDao.selectUserInforByWechatUser();
    }

    @Override
    public List<BsUserInfor> selectUserInforInNeedInfor(String needId) {
        return this.userInforSelfDao.selectUserInforInNeedInfor(needId);
    }


    @Override
    public List<BsUserDetailInfo> selectUserDetail(BsUserDetailInfo detailInfo) {
        return userInforDao.selectUserDetail(detailInfo);
    }

    @Override
    public int updateUserStatus(BsUserInfor infor) {
        return userInforDao.updateUserStatus(infor);
    }

    @Override
    public int updateUserByUserNo(BsUserInfor infor) {
        return userInforDao.updateUserByUserNo(infor);
    }
    
    /**
     * 用户后台登录方法
     * @param userName
     * @param password
     * @return
     */
    public List<BsUserInfor> adminLogin(String phoneNo,String password){
    	return userInforDao.adminLogin(phoneNo, password);
    }

	@Override
	public List<BsUserDetailInfo> selectNoPassRegUserByRouid(String rouid) {
		return userInforDao.selectNoPassRegUserByRouid(rouid);
	}

	@Override
	public int updateOpenidtoNullByRouid(String rouid) {
		return userInforDao.updateOpenidtoNullByRouid(rouid);
	}
}
